home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / intl / collation / demos-1.1 / TextBoundDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  12.3 KB  |  451 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /*
  18.  * @(#)TextBoundDemo.java    1.1 96/11/23
  19.  *
  20.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  21.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  22.  *
  23.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  24.  *
  25.  *   The original version of this source code and documentation is copyrighted
  26.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  27.  * materials are provided under terms of a License Agreement between Taligent
  28.  * and Sun. This technology is protected by multiple US and International
  29.  * patents. This notice and attribution to Taligent may not be removed.
  30.  *   Taligent is a registered trademark of Taligent, Inc.
  31.  *
  32.  * Permission to use, copy, modify, and distribute this software
  33.  * and its documentation for NON-COMMERCIAL purposes and without
  34.  * fee is hereby granted provided that this copyright notice
  35.  * appears in all copies. Please refer to the file "copyright.html"
  36.  * for further important copyright and licensing information.
  37.  *
  38.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  39.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  40.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  41.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  42.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  43.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  44.  *
  45.  */
  46.  
  47. import java.applet.Applet;
  48. import java.awt.*;
  49. import java.util.*;
  50.  
  51. import java.text.BreakIterator;
  52.  
  53. public class TextBoundDemo extends DemoApplet
  54. {
  55.     public static void main(String argv[]) {
  56.         DemoApplet.showDemo(new TextBoundFrame(null));
  57.     }
  58.  
  59.     public Frame createDemoFrame(DemoApplet applet) {
  60.         return new TextBoundFrame(applet);
  61.     }
  62. }
  63.  
  64.  
  65.  
  66. class TextBoundFrame extends Frame
  67. {
  68.     private static final String creditString =
  69.         "v1.1a9, Demo";
  70.  
  71.     private static final int FIELD_COLUMNS = 45;
  72.     private static final Font choiceFont = null;
  73.     private static final boolean DEBUG = false;
  74.     private DemoApplet applet;
  75.  
  76.     final String right = "-->";
  77.     final String left = "<--";
  78.  
  79.     private BreakIterator enum;
  80.  
  81.     TextArea text;
  82.     Choice bound;
  83.  
  84.     public TextBoundFrame(DemoApplet applet)
  85.     {
  86.         this.applet = applet;
  87.         init();
  88.         start();
  89.     }
  90.  
  91.  
  92.  
  93.     public void run()
  94.     {
  95.         /*
  96.         while (true) {
  97.             try {
  98.                 checkChange();
  99.                 Thread.sleep(250);
  100.             }
  101.             catch (InterruptedException e) {
  102.             }
  103.             catch (Exception e) {
  104.             }
  105.             catch (Throwable e) {
  106.             }
  107.         }
  108.         */
  109.     }
  110.  
  111.     int s, e;
  112.     int ts, te;
  113.  
  114.     public void checkChange()
  115.     {
  116. //        System.out.println("checkChange...");
  117.         if ((text.getSelectionStart() & 0x7FFF) != ts ||
  118.             (text.getSelectionEnd() & 0x7FFF) != te) {
  119.  
  120.             int tempS = text.getSelectionStart() & 0x7FFF;
  121.             int tempE = text.getSelectionEnd() & 0x7FFF;
  122.  
  123. //            System.out.println(">");
  124. //            select(0, 0);
  125. //            select(tempS, tempE);
  126.             select(tempS - (ts - s), tempE - (te - e));
  127. //            System.out.println("<");
  128.  
  129.  
  130. //           if (s != ts || e != te) System.out.println("     s("+s+") ts("+ts+") e("+e+") te("+te+")");
  131. //           if (tempS != ts || tempE != te) System.out.println(">s("+s+") tempS("+tempS+") e("+e+") tempE("+tempE+")");
  132. //            select(s - (ts - s), e - (te - e));
  133. //            if (tempS != ts || tempE != te) System.out.println("s("+s+") tempS("+tempS+") e("+e+") tempE("+tempE+")");
  134.  
  135. //            System.out.println("lkdslksj");
  136.         }
  137.     }
  138.  
  139.     public void select(int sIn, int eIn)
  140.     {
  141.         s = sIn;
  142.         e = eIn;
  143.         text.select(s, e);
  144.         ts = text.getSelectionStart() & 0x7FFF;
  145.         te = text.getSelectionEnd() & 0x7FFF;
  146.         // System.out.println("   "+(ts-s)+","+(te-e));
  147.         // System.out.println(">>select");
  148.            // if (s != ts || e != te) System.out.println(">s("+s+") ts("+ts+") e("+e+") te("+te+")");
  149. //          if (s != ts || e != te) System.out.println(">>s("+s+") ts("+ts+") e("+e+") te("+te+")");
  150.     }
  151.  
  152.     public int getSelectionStart()
  153.     {
  154.         checkChange();
  155.         return s;
  156. //        return text.getSelectionStart() & 0x7FFF;
  157.     }
  158.  
  159.  
  160.     public int getSelectionEnd()
  161.     {
  162.         checkChange();
  163.         return e;
  164. //        return text.getSelectionEnd() & 0x7FFF;
  165.     }
  166.  
  167.     public final synchronized void selectRange(int s, int e)
  168.     {
  169.         try {
  170.             //if (getSelectionStart() != s || getSelectionEnd() != e) {
  171.                 //text.select(s, e);
  172.                 select(s,e);
  173.             //}
  174. //            if (getSelectionStart() != s || getSelectionEnd() != e) {
  175. //                System.out.println("AGH! select("+s+","+e+") -> ("+
  176. //                getSelectionStart()+","+getSelectionEnd()+")");
  177. //                text.select(s - (getSelectionStart() - s), e - (getSelectionEnd() - e));
  178. //            }
  179.         } catch (Exception exp) {
  180.             errorText(exp.toString());
  181.         }
  182.     }
  183.  
  184.  
  185.  
  186.     public void init()
  187.     {
  188.         buildGUI();
  189.     }
  190.  
  191.  
  192.     public void start()
  193.     {
  194.     }
  195.  
  196.  
  197.     void addWithFont(Container container, Component foo, Font font) {
  198.         if (font != null)
  199.             foo.setFont(font);
  200.         container.add(foo);
  201.     }
  202.  
  203.  
  204.  
  205.    public void buildGUI()
  206.     {
  207.         setBackground(Utility.bgColor);
  208.         setLayout(new BorderLayout());
  209.  
  210.        Panel topPanel = new Panel();
  211.  
  212.             Label titleLabel =
  213.                 new Label("Text Boundary Demo", Label.CENTER);
  214.             titleLabel.setFont(Utility.titleFont);
  215.             topPanel.add(titleLabel);
  216.  
  217.             Label demo=new Label(creditString, Label.CENTER);
  218.             demo.setFont(Utility.creditFont);
  219.             topPanel.add(demo);
  220.  
  221.             Panel choicePanel = new Panel();
  222.  
  223.             Label demo1=new Label("Boundaries", Label.LEFT);
  224.             demo1.setFont(Utility.labelFont);
  225.             choicePanel.add(demo1);
  226.  
  227.             bound = new Choice();
  228.                 bound.setBackground(Utility.choiceColor);
  229.             bound.addItem("Sentence");
  230.             bound.addItem("Line Break");
  231.             bound.addItem("Word");
  232.             bound.addItem("Char");
  233.             if (choiceFont != null)
  234.                 bound.setFont(choiceFont);
  235.  
  236.             choicePanel.add(bound);
  237.             topPanel.add(choicePanel);
  238.  
  239.             Utility.fixGrid(topPanel,1);
  240.  
  241.  
  242.         add("North", topPanel);
  243.  
  244.  
  245.             int ROWS = 15;
  246.              int COLUMNS = 50;
  247.             text = new TextArea(getInitialText(), ROWS, COLUMNS);
  248.             text.setEditable(true);
  249.             text.selectAll();
  250.             text.setFont(Utility.editFont);
  251.         add("Center", text);
  252.  
  253.         Panel copyrightPanel = new Panel();
  254.         addWithFont (copyrightPanel,
  255.             new Label(Utility.copyright1, Label.LEFT),Utility.creditFont);
  256.         addWithFont (copyrightPanel,
  257.                new Label(Utility.copyright2, Label.LEFT),Utility.creditFont);
  258.         Utility.fixGrid(copyrightPanel,1);
  259.         add("South", copyrightPanel);
  260.  
  261.         //layout();
  262.         handleEnumChanged();
  263.  
  264.         // (new Thread(this)).start();
  265.     }
  266.  
  267.  
  268.  
  269.     public String getInitialText()
  270.     {
  271.         return
  272.     /*
  273.             "\"This is a sentence.\" This is not.\" \"because. And go. " +
  274.             "This is a simple 012.566,5 sample sentence. \n"+
  275.             "It does not have to make any sense as you can see. \n"+
  276.             "Nel mezzo del cammin di nostra vita, mi ritrovai in "+
  277.                 "una selva oscura. \n"+
  278.             "Che la dritta via aveo smarrita. \n"+
  279.             "He said, that I said, that you said!! \n"+
  280.             "Don't rock the boat.\n\n"+
  281.             "Because I am the daddy, that is why. \n"+
  282.             "Not on my time (el timo.)! \n"+
  283.             "Tab\tTab\rTab\tWow."+
  284.             "So what!!\n\n"+
  285.             "Is this a question???  " +
  286.             "I wonder...Hmm.\n" +
  287.             "Harris thumbed down several, including \"Away We Go\" "+
  288.                 "(which became the huge success Oklahoma!). \n"+
  289.             "One species, B. anthracis, is highly virulent.\n"+
  290.             "Wolf said about Sounder: \"Beautifully thought-out and "+
  291.                 "directed.\"\n"+
  292.             "Have you ever said, \"This is where I shall live\"? \n"+
  293.             "He 1000,233,456.000 answered, \"You may not!\" \n"+
  294.             "Another popular saying is: \"How do you do?\". \n"+
  295.             "What is the proper use of the abbreviation pp.? \n"+
  296.             "Yes, I am 1,23.322% definatelly 12\" tall!!";
  297.     */
  298.             "(\"This is a complete sentence.\") This is (\"not.\") also. \n"
  299.             +"An abbreviation in the middle, etc. and one at the end, etc. "+
  300.                 "This\n"
  301.             +"is a simple sample 012.566,5 sentence. It doesn't\n"
  302.             +"have to make any sense, as you can see. Nel mezzo del \nc"
  303.             +"ammin di nostra vita, mi ritrovai in una selva oscura. Che\n"
  304.             +"la dritta via aveo smarrita. Not on my time (el timo.)! And\n"
  305.             +"tabulated columns: \tCol1\tCol2\t3,456%.\t\n"
  306.             +"Is this a question???  I wonder... Hmm. Harris thumbed\n"
  307.             +"down several, including \"Away We Go\" (which became the \n"
  308.             +"huge success Oklahoma!). One species, B. anthracis, is \n"
  309.             +"highly virulent. Wolf said about Sounder: \"Beautifully \n"
  310.             +"thought-out and directed.\" Have you ever said, \"This is "+
  311.                 "where I\n"
  312.             +"shall live\"? He said 1000,233,456.000 and answered, \"You "+
  313.                 "may not!\" \n"
  314.             +"Another popular saying is: \"How do you do?\". What is the \n"
  315.             +"proper use of the abbreviation pp.? Yes, I am 12\' 3\" tall!!";
  316.     }
  317.  
  318.  
  319.     public void handleEnumChanged()
  320.     {
  321.         String s = bound.getSelectedItem();
  322.         if (s.equals("Char")) {
  323.             errorText("getCharacterInstance");
  324.             enum = BreakIterator.getCharacterInstance();
  325.         }
  326.         else if (s.equals("Word")) {
  327.             errorText("tWordBreak");
  328.             enum = BreakIterator.getWordInstance();
  329.         }
  330.         else if (s.equals("Line Break")) {
  331.             errorText("getLineInstance");
  332.             enum = BreakIterator.getLineInstance();
  333.         }
  334.         else /* if (s.equals("Sentence")) */ {
  335.             errorText("getSentenceInstance");
  336.             enum = BreakIterator.getSentenceInstance();
  337.         }
  338.         enum.setText(text.getText());
  339.         selectRange(0, 0);
  340.         //text.select(0,0);
  341.     }
  342.  
  343.     public void handleForward()
  344.     {
  345.         try {
  346.             enum.setText(text.getText());
  347.             int oldStart = getSelectionStart();
  348.             int oldEnd = getSelectionEnd();
  349.             if (oldEnd < 1) {
  350.                 selectRange(0, enum.following(0));
  351.             }
  352.             else {
  353.                 int s = enum.following(oldEnd-1);
  354.                 int e = enum.next();
  355.                 if (e == -1) {
  356.                     e = s;
  357.                 }
  358.                 selectRange(s, e);
  359.             }
  360.             //text.select(s, e);
  361.             errorText("<" + oldStart + "," + oldEnd + "> -> <" +
  362.                 s + "," + e + ">");
  363.         }
  364.         catch (Exception exp) {
  365.             errorText(exp.toString());
  366.         }
  367.     }
  368.  
  369.     public void handleBackward()
  370.     {
  371.         try {
  372.             enum.setText(text.getText());
  373.             int oldStart = getSelectionStart();
  374.             int oldEnd = getSelectionEnd();
  375.             if (oldStart < 1) {
  376.                 selectRange(0, 0);
  377.             }
  378.             else {
  379.                 int e = enum.following(oldStart-1);
  380.                 int s = enum.previous();
  381.                 selectRange(s, e);
  382.             }
  383.             //text.select(s, e);
  384.             errorText("<" + oldStart + "," + oldEnd + "> -> <" + s + "," + e + ">");
  385.         }
  386.         catch (Exception exp) {
  387.             errorText(exp.toString());
  388.         }
  389.     }
  390.  
  391.     public boolean action(Event evt, Object obj)
  392.     {
  393.  
  394.         if(evt.target instanceof Button && left.equals(obj))
  395.         {
  396.             handleBackward();
  397.             return true;
  398.         }
  399.         else if(evt.target instanceof Button && right.equals(obj))
  400.         {
  401.             handleForward();
  402.             return true;
  403.         }
  404.         else if(evt.target instanceof Choice)
  405.         {
  406.             handleEnumChanged();
  407.             return true;
  408.         }
  409.         return false;
  410.     }
  411.  
  412.     public boolean handleEvent(Event evt)
  413.     {
  414.         /*
  415.         if (evt.id == Event.KEY_RELEASE || evt.id == Event.KEY_PRESS) {
  416.             return true;
  417.         }
  418.         else
  419.         */
  420.         if (evt.id == Event.KEY_PRESS || evt.id == Event.KEY_ACTION) {
  421.             //super.handleEvent(evt);
  422.             switch (evt.key) {
  423. //            case '.':
  424.             case 0x3ef:
  425.                 handleForward();
  426.                 return true;
  427. //            case ',':
  428.             case 0x3ee:
  429.                 handleBackward();
  430.                 return true;
  431.             }
  432.         }
  433.         else
  434.         if (evt.id == Event.WINDOW_DESTROY && evt.target == this) {
  435.             this.setVisible(false);
  436.             this.dispose();
  437.                 if (applet != null) {
  438.                   applet.demoClosed();
  439.                } else System.exit(0);
  440.             return true;
  441.         }
  442.         return super.handleEvent(evt);
  443.     }
  444.  
  445.     public void errorText(String s)
  446.     {
  447.        if (DEBUG)
  448.            System.out.println(s);
  449.     }
  450. }
  451.